
const { app, BrowserWindow , ipcMain} = require('electron')
const path = require('path')
const file = require('fs')
var md = require('markdown-it')();

// dev/dist mode identify
const fileAssociate = app.isPackaged? process.argv[1] : process.argv[2]
const filename = fileAssociate.length > 0? path.basename(fileAssociate) : ''

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    }
  })

  const content = win.webContents
  content.on('dom-ready', ()=>{
    let html = md.render(file.readFileSync(fileAssociate, 'utf-8'))
    content.send('filename', html, filename)
    if (!app.isPackaged) {
      content.openDevTools()
    }

  })

  win.loadFile('index.html')
  return win
}

app.whenReady().then(() => {

  ipcMain.handle('rendermd', async (event, text)=>{
      const result = md.render(text)
      return result
  })
  createWindow()
})

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit()
  
})
